import { message } from 'antd';
import {
  getDetail,
  getSendAddEmpDetail,
  getPayDetail,
  detailreturnReceipt,
  modifyPreser,
  lookagreement,
  getbatchConnec,
} from '@/services/adjusttaskrec';

export default {
  state: {
    sendOrdAddTaskId: null,
    responseSendOrdDetailDto: null, // 派单信息
    empInfoDto: null, // 员工信息
    pdInfoDtos: null, // 产品服务信息
    protInfoDto: null, // 协议信息
    addEmpDetSerDto: null, // 社保公积金信息
    assDetailDtoList: null, // 增加险种信息
    execStatusDto: null, // 执行状态
    dataDtos: null, // 材料信息
    addEmpDetContRecDto: null, // 联系记录
    handDto: null, // 办理备注
    probDets: null, // 问题记录
    payDetailList: [], // 缴纳明细
    navNum: '1', // 导航数量

    isUpdate: false, // 是否修改页面
    insIdeUpdateFlag: false, // 是否修改缴费人员类别
    insWageFlag: false, // 是否修改社保工资
    insSetFlag: 2, // 五险合不合一
    insLocFlag: false, // 是否修改社保组合
    accuWageFlag: false, // 是否修改公积金申报工资
    accuLocFlag: false, // 是否修改公积金组合

    updateBaseInfo: null, // 基本信息
    beginYm: null, // 生效年月
    injLocPkgDesc: null, // 修改说明
    updateAdjInsLocPkgList: null, // 社保组合
    updateInsIde: null, // 缴费人员类别
    updateInsPdDecWage: null, // 社保申报工资
    updateAccuDecWage: null, // 公积金申报工资对象
    updateAccuLocPkg: null, // 公积金组合对象
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    coltFlag: 1, // 是否需要材料信息
  },
  effects: {
    // 详情数据
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(getDetail, payload);
      if (response.success) {
        // if (response.data.execStatusDto && response.data.execStatusDto.leafList) {
        //   response.data.execStatusDto.leafList = response.data.execStatusDto.leafList.sort(
        //     sortNumber
        //   );
        // }
        yield put({
          type: 'updateModelData',
          payload: {
            ...payload,
            ...response.data,
          },
        });
      } else {
        message.warning(response.message || '获取人员信息：服务端错误');
      }
    },

    // 修改回显数据
    *fetchEmpDetail({ payload }, { call, put }) {
      const response = yield call(getSendAddEmpDetail, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            ...payload,
            ...response.data,
          },
        });
      } else {
        message.warning(response.message || '获取人员信息：服务端错误');
      }
    },

    // 回单
    *returnReceipt({ payload, callback }, { call }) {
      const response = yield call(detailreturnReceipt, payload);
      if (response.success) {
        message.success('回单成功！');
        callback(response);
      } else {
        message.warning(response.message || '回单失败：服务端错误');
      }
    },

    // 获取缴纳明细数据
    *fetchPayDetail({ payload }, { call, put }) {
      const response = yield call(getPayDetail, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { payDetailList: (response.data.length && response.data) || [] },
        });
      } else {
        message.warning(response.message || '获取缴费明细信息：服务端错误');
      }
    },

    // 修改保存
    // eslint-disable-next-line consistent-return
    *submissionPreser({ payload }, { call, put }) {
      const response = yield call(modifyPreser, payload);
      if (response.success) {
        message.success('修改成功！');
        yield put({
          type: 'fetchEmpDetail',
          payload: { recvOrdAdjTaskId: payload.recvOrdAdjTaskId },
        });
        return response;
      }
      message.warning(response.message || '修改失败：服务端错误');
    },

    // 查看协议
    *seeagreement({ payload, callback }, { call, put }) {
      // debugger
      const response = yield call(lookagreement, payload);
      if (response.success) {
        yield put({
          type: 'getseeagreementList',
          payload: { data: response.data },
        });
      } else {
        yield put({
          type: 'getseeagreementList',
          payload: { data: [] },
        });
      }
      callback(response);
    },

    // 批量接单
    *batchConnec({ payload, callback }, { call }) {
      const response = yield call(getbatchConnec, payload);
      if (response.success) {
        message.success('接单成功！');
        callback(response);
      } else {
        message.warning(response.message || '接单失败！');
        callback(response);
      }
    },
  },

  reducers: {
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
        // navNum: num,
      };
    },
    controlNavnum(state) {
      return {
        ...state,
        navNum: '1',
      };
    },
    // 查看协议
    getseeagreementList(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        data: {
          list,
          pagination: {
            current: 1,
            pageSize: 10,
            total: 10,
          },
        },
      };
    },
  },
};
